import { Component, OnInit } from '@angular/core';
import { OrderService } from 'src/app/services/order.service';
import * as constants from '../../../providers/Constants'
import { Storage } from 'src/app/providers/Storage';
import { ToastService } from 'src/app/providers/toast/toast.service';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import { Utils } from 'src/app/providers/Utils';

@Component({
  selector: 'app-payment-type',
  templateUrl: './payment-type.page.html',
  styleUrls: ['./payment-type.page.scss'],
})
export class PaymentTypePage implements OnInit {
  cardList:any=[];
  cardType:string='';
  constructor(private order:OrderService,private toast:ToastService,private nav:NavController,private activatedRoute:ActivatedRoute ) { 
    this.activatedRoute.queryParams.subscribe((params: Params) => {
      if (params.type) {
        this.cardType=params.type;
      }
      this.initCard();
    
    });
  }
  // onClickUrl(){
  //   this.nav.navigateBack('payment-add-card');
  // }
  ngOnInit() {
  
   
  }
  utilsCard(str){
    return Utils.utilsCard(str)
  }
  onClickItem(item){
    if(this.cardType){
      Storage.sessionStorage.set('backCard',item);
      // this.toast.show(constantEN.publicUtils.paySucceed);
      this.nav.pop();
    }else{
      // this.nav.navigateForward('payment-add-card',{
      //   queryParams:{
      //     itemData:JSON.stringify(item)
      //   }
      // });
    }

  }
  initCard(){
    //查询银行卡列表
    this.order.getSelCard({}).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        console.log('银行卡列表',res);
        //if(res.array.length>0){
          this.cardList=res.array;
       // }
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }

  del(item){
    console.log("要删除的数据",item.cardId)

    this.order.delCard({cardId:item.cardId}).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
              this.initCard();
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });

  }
}
